package com.unione.heathrobot;

import static com.unione.heathrobot.constant.AppConstant.SP_KEY_CHARGE_BATTERY;
import static com.unione.heathrobot.constant.AppConstant.SP_KEY_LOWBATTERY;
import static com.unione.heathrobot.constant.AppConstant.default_charge_battery;
import static com.unione.heathrobot.constant.AppConstant.default_lowbattery;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.unione.healthrobot.R;
import com.unione.unione_voice.utils.SPUtil;

public class SettingDialog extends Dialog implements View.OnClickListener {

    private EditText etLowBattery;
    private EditText etHighBattery;
    private AppCompatButton btnConfirm;
    private Context mContext;

    public SettingDialog(@NonNull Context context) {
        super(context);
        setContentView(R.layout.layout_set_battery);
        mContext = context;
        etHighBattery = findViewById(R.id.etHighBattery);
        etLowBattery = findViewById(R.id.etLowBattery);
        btnConfirm = findViewById(R.id.btnConfirm);
        btnConfirm.setOnClickListener(this);
        etLowBattery.setText(String.valueOf(SPUtil.getInt(
                context,
                SP_KEY_LOWBATTERY,
                default_lowbattery
        )));
        etHighBattery.setText(String.valueOf(SPUtil.getInt(context, SP_KEY_CHARGE_BATTERY, default_charge_battery)));
    }


    @Override
    public void onClick(View v) {
        String lowBattery = etLowBattery.getText().toString().trim();
        String highBattery = etHighBattery.getText().toString().trim();
        if (lowBattery.isBlank() || highBattery.isBlank()) {
            ToastUtils.showLong("请输入正确的电量");
        } else {
            SPUtil.putInt(
                    mContext,
                    SP_KEY_LOWBATTERY,
                    Integer.parseInt(lowBattery)
            );
            SPUtil.putInt(mContext, SP_KEY_CHARGE_BATTERY, Integer.parseInt(highBattery));
            dismiss();
        }

    }
}
